const request = require('request')

// chenhaojie 子账号
// const AK = "uwqPd0EgIMkOdbFY3lsBVyQD"
// const SK = "OclBp18vxcqgP3ECIXrO8AMpuYXwcMwm"

// tr 子账号
const AK = "z740YEZU7LnNiQWyrp6yLY5P"
const SK = "KXzRAbv0tnnOEgPkvoGf879R8WsbGGM8"

// 获取AccessToken
const getAccessToken = function () {
    const options = {
        'method': 'POST',
        'url': 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + AK + '&client_secret=' + SK,
    }
    return new Promise((resolve, reject) => {
        request(options, (error, response) => {
            if (error) {
                reject(error)
            } else {
                resolve(JSON.parse(response.body).access_token)
            }
        })
    })
}

// 人脸搜索1:N识别
const faceSearchV3 = async function (groupIdList, base64Image, qualityControl = 'NORMAL', maxUserNum = 10) {
    const options = {
        'method': 'POST',
        'url': 'https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=' + await getAccessToken(),
        'headers': {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            "group_id_list": groupIdList,
            "image": base64Image,
            "image_type": "BASE64",
            "quality_control": qualityControl,
            "max_user_num": maxUserNum
        })
    };
    return new Promise((resolve, reject) => {
        request(options, (error, response) => {
            if (error) {
                reject(error)
            } else {
                resolve(JSON.parse(response.body))
            }
        })
    })
}

// 人脸质量检测
const faceDetectV3 = async function (base64Image, faceField = 'quality') {
    const options = {
        'method': 'POST',
        'url': 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + await getAccessToken(),
        'headers': {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            "image": base64Image,
            "image_type": "BASE64",
            "face_field": faceField
        })
    };
    return new Promise((resolve, reject) => {
        request(options, (error, response) => {
            if (error) {
                reject(error)
            } else {
                resolve(JSON.parse(response.body))
            }
        })
    })
}

// 人脸注册
const addUser = async function (groupId, userId, userInfo, base64Image) {
    const options = {
        'method': 'POST',
        'url': 'https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=' + await getAccessToken(),
        'headers': {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            "group_id": groupId,
            "user_id": userId,
            "image": base64Image,
            "user_info": userInfo,
            "image_type": "BASE64"
        })
    }
    return new Promise((resolve, reject) => {
        request(options, (error, response) => {
            if (error) {
                reject(error)
            } else {
                resolve(JSON.parse(response.body))
            }
        })
    })
}

module.exports = {
    addUser,
    faceSearchV3,
    faceDetectV3,
}
